package com.cuij.servicelmpl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cuij.dao.orderBusiDao;
import com.cuij.dao.orderCustDao;
import com.cuij.dao.orderUserDao;
import com.cuij.entity.Result;
import com.cuij.entity.orderBusi;
import com.cuij.entity.orderCust;
import com.cuij.entity.orderUser;
import com.cuij.service.orderUserService;
import com.cuij.util.DateUtil;
import com.cuij.util.UUIDUtil;

@Service
public class orderUserServicelmpl implements orderUserService {

    @Resource
    private orderUserDao userdao;
    @Resource
    private orderBusiDao busidao;
    @Resource
    private orderCustDao custdao;

    // 登录
    public Result login(String username, String password, String custType) {
        Result result = new Result();
        orderUser orderuser = userdao.findbyusername(username);
        if (orderuser == null) {
            result.setStatus(1);
            result.setMsg("该用户不存在");
        }
        else {
            // 获取账户信息
            String Dateuserid = orderuser.getUse_id();
            String DateUsername = orderuser.getUse_name();
            String Datepassword = orderuser.getUse_pwd();
            String DatecustType = orderuser.getCust_type();
            // 查询客户ID
            Map resMap = new HashMap();
            String cust_id = custdao.querycustid(Dateuserid);
            resMap.put("cust_id", cust_id);
            resMap.put("use_id", Dateuserid);
            if (!(username.equals(DateUsername)) || !(password.equals(Datepassword))) {
                result.setStatus(2);
                result.setMsg("账户名或密码错误");
            }
            else if (!(custType.equals(DatecustType))) {
                result.setStatus(3);
                result.setMsg("用户权限不一致");
            }
            else {
                result.setStatus(0);
                result.setMsg("登录成功");
                result.setData(resMap);
            }
        }
        return result;
    }

    // 客户注册
    public Result custsave(String use_name, String use_pwd) {
        Result result = new Result();
        orderUser checkuser = userdao.findbyusername(use_name);
        orderUser cust = new orderUser();
        if (checkuser != null) {
            result.setStatus(1);
            result.setMsg("该用户已存在");
        }
        else {
            String use_id = UUIDUtil.creatUUID();
            String cust_id = UUIDUtil.creatUUID();
            String cust_type = "1";
            String create_time = DateUtil.getCurrentTime();
            // 入库cust基本信息库
            orderCust ordercust = new orderCust();
            ordercust.setCust_id(cust_id);
            ordercust.setUse_id(use_id);
            custdao.savecustid(ordercust);
            // 注册成功
            cust.setUse_id(use_id);
            cust.setUse_name(use_name);
            cust.setUse_pwd(use_pwd);
            cust.setCust_type(cust_type);
            cust.setCreate_time(create_time);
            userdao.custresign(cust);

            result.setStatus(0);
            result.setMsg("注册成功");
            result.setData(use_id);
        }
        return result;
    }

    public Result busisave(String use_name, String use_pwd, String busi_name, String busi_pic, String busi_type,
            String busi_linkname, String busi_linkphone, String busi_address) {
        Result result = new Result();
        // 查询是否用户已存在
        orderUser busiInfo = userdao.findbyusername(use_name);
        if (busiInfo != null) {
            result.setStatus(1);
            result.setMsg("该用户已存在");
        }
        else {
            orderUser busiresign = new orderUser();
            orderBusi busi = new orderBusi();
            // 注册的入库操作
            String use_id = UUIDUtil.creatUUID();
            String cust_type = "2";
            String create_time = DateUtil.getCurrentTime();
            busiresign.setUse_id(use_id);
            busiresign.setUse_name(use_name);
            busiresign.setUse_pwd(use_pwd);
            busiresign.setCust_type(cust_type);
            busiresign.setCreate_time(create_time);
            userdao.custresign(busiresign);
            // 将商家的信息入库
            String busi_id = UUIDUtil.creatUUID();
            busi.setBusi_id(busi_id);
            busi.setBusi_name(busi_name);
            busi.setUse_id(use_id);
            busi.setBusi_pic(busi_pic);
            busi.setBusi_type(busi_type);
            busi.setBusi_linkname(busi_linkname);
            busi.setBusi_linkphone(busi_linkphone);
            busi.setBusi_address(busi_address);
            // 入库默认信息
            double busi_money = 0;
            double busi_level = 0;
            int busi_count = 0;
            String busi_speed = "0";
            busi.setBusi_level(busi_level);
            busi.setBusi_speed(busi_speed);
            busi.setBusi_money(busi_money);
            busi.setBusi_count(busi_count);
            busidao.saveBusiInfo(busi);

            // 返回结果
            result.setStatus(0);
            result.setMsg("注册成功");
        }

        return result;
    }

    // 修改密码
    public Result changepwd(String use_id, String old_pwd, String new_pwd) {
        Result result = new Result();
        // 查询原密码
        String use_pwd = userdao.querypwd(use_id);
        if (!use_pwd.equals(old_pwd)) {
            result.setStatus(1);
            result.setMsg("您输入的原密码不正确，请重新输入");
        }
        else {
            orderUser orderuser = new orderUser();
            orderuser.setUse_id(use_id);
            orderuser.setUse_pwd(new_pwd);
            userdao.changepwd(orderuser);
            result.setStatus(0);
        }
        return result;
    }

}
